package cn.xx.pet.service.impl;

import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.org.domain.Shop;
import cn.xx.pet.domain.MasterMsg;
import cn.xx.pet.domain.Pet;
import cn.xx.pet.domain.PetDetail;
import cn.xx.pet.domain.PetType;
import cn.xx.pet.mapper.PetDetailMapper;
import cn.xx.pet.mapper.PetMapper;
import cn.xx.pet.mapper.PetTypeMapper;
import cn.xx.pet.service.IPetService;
import cn.xx.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetTypeMapper petTypeMapper;




    @Override
    public List<Shop> findShops() {
        return petMapper.findShops();
    }

    @Override
    public List<MasterMsg> findMasters() {
        return petMapper.findMasters();
    }

    @Override
    public List<PetType> findTypes() {
        return petMapper.findTypes();
    }

    @Override
    public void save(Pet pet) {
        // 模拟发布人id为0
        pet.setSearch_master_msg(0L);
        // 模拟状态为下架
        pet.setState(0);
        // 默认花费100
        pet.setCostprice("100");
        // 调用父类方法保存
        super.save(pet);
        // 设置详细表外键id
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet(pet);
        // 设置类型表外键id
        PetType petType = pet.getPetType();
        petType.setPet(pet);
        // 保存详细信息
        petDetailMapper.save(petDetail);
        // 保存类型信息
        petTypeMapper.save(petType);
    }

    @Override
    public void update(Pet pet) {
        // 调用父类方法修改
        super.update(pet);
        // 设置详细表外键id
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet(pet);
        // 设置类型表外键id
        PetType petType = pet.getPetType();
        petType.setPet(pet);
        // 修改详细信息
        petDetailMapper.update(petDetail);
        // 修改类型信息
        petTypeMapper.update(petType);
    }

    @Override
    public void onSale(Long[] ids) {
        petMapper.onSale(ids);
    }

    @Override
    public void offSale(Long[] ids) {
        petMapper.offSale(ids);
    }
}
